package com.base.controller;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.helper.VosHelper;
import com.base.po.param.InfoCustomers;
import com.base.po.param.VosModelList;
import com.base.po.voip.Smsconfigs;
import com.base.po.voip.SmsconfigsExample;
import com.base.po.voip.Users;
import com.base.po.voip.UsersExample;
import com.base.po.voip.UsersExample.Criteria;
import com.base.service.CallbackinfoesService;
import com.base.service.LogsService;
import com.base.service.SmsconfigsService;
import com.base.service.UsersService;
import com.base.utils.GsonUtil;
import com.base.utils.IP;
import com.base.utils.PasswordUtils;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_caiwuxinxi {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private UsersService usersService;
	@Autowired
	private SmsconfigsService smsconfigsService;// 短信配置
	@Autowired
	private CallbackinfoesService callbackinfoesService;// 话单记录

	// OEM管理
	@RequestMapping("/content_caiwuxinxi")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		UsersExample example= new UsersExample();
		
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		List<Users> users = null;
		Criteria criteria = example.createCriteria();
		switch (adminUsergrade) {
		case 0:
			example.getOredCriteria().add(criteria.andRoleEqualTo(0).andIdEqualTo(adminUserId));
			break;
		case 1:
			example.getOredCriteria().add(criteria.andRoleEqualTo(1).andIdEqualTo(adminUserId));
			break;
		case 2:
			example.getOredCriteria().add(criteria.andRoleEqualTo(2).andIdEqualTo(adminUserId));
			break;
		}
		users = usersService.selectByExample(example);
		if (users != null) {
			for (Users u : users) {
				// 设置剩余短信
				SmsconfigsExample smsconfigsExample = new SmsconfigsExample();
				smsconfigsExample.getOredCriteria().add(
						smsconfigsExample.createCriteria().andAgentidEqualTo(u.getId()).andDatastatusEqualTo(true));
				List<Smsconfigs> sms = smsconfigsService.selectByExample(smsconfigsExample);
				if (sms != null && sms.size() > 0) {
					u.setAppstore(sms.get(0).getCount() + "");
				} else {
					u.setAppstore("0");
				}
				DecimalFormat df = new DecimalFormat("#0.00");
				// 设计系统消耗
				BigDecimal xh = callbackinfoesService.sumOmoneyParentID(u.getId());
				if (xh == null) {
					u.setKeypath("0");
				} else {
					u.setKeypath(df.format(xh) + "");
				}
				u.setBlance(new BigDecimal(df.format(u.getBlance())));
				// 设置话费余额，以及透支
				String results = VosHelper.GetVosAccount("{accounts:['" + u.getUsername() + "']}");
				String result1 = VosHelper.GetVosAccount("{accounts:['" + u.getUsername() + "j']}");
				VosModelList v1 = GsonUtil.getObject(results, VosModelList.class);
				VosModelList v2 = GsonUtil.getObject(result1, VosModelList.class);
				if (v2 != null && v2.infoCustomers.length != 0 && v1 != null && v1.infoCustomers.length != 0) {
					InfoCustomers info2 = v2.infoCustomers[0];
					InfoCustomers info1 = v1.infoCustomers[0];
					u.setLinebalance(new BigDecimal(new DecimalFormat("0.00").format(info2.getMoney())));// 线路费
					u.setOverdraft(info2.getLimitMoney().intValue());// 透支
					u.setFreeroute(info1.getFeeRateGroup());// 费率，数据库中已经保存
				} else {
					u.setLinebalance(new BigDecimal(0.00));
					u.setOneagent("0.00");
				}
				u.setPassword(PasswordUtils.decodePWD(u.getPassword()));// 设置明文密码
			}
			Users user = usersService.selectByPrimaryKey(adminUserId);
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_caiwuxinxi");
			request.setAttribute("auth", 4);
			request.setAttribute("list", users);
			request.setAttribute("adminUsergrade", adminUsergrade);
			request.setAttribute("userName", user.getUsername());
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "流量代理商信息", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
}
